package com.lbs.jiepiaisi.view;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.activity.VisitTypeActivity;
import com.lbs.jiepiaisi.bean.AnswerQuestionBean;
import com.lbs.jiepiaisi.bean.QuestionRequestBean;
import com.lbs.jiepiaisi.utils.ButtonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ydm
 * @date 2018/2/20
 * @desc
 */

public class HomeListView extends LinearLayout {

    private Context mContext;

    private LinearLayout mLinOut;
    private List<HomeSingleView> homeSingleViews = new ArrayList<>();

    public HomeListView(Context context) {
        super(context);
        initView(context);
    }

    public HomeListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HomeListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){
        this.mContext = context;
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.view_home_question_list, this);
        // 获取控件
        mLinOut = (LinearLayout)findViewById(R.id.lin_out);
    }

    public void setQuestionList(List<AnswerQuestionBean.Result> results){
        homeSingleViews.clear();
        int i = 1;
        for(AnswerQuestionBean.Result result : results){
            HomeSingleView singleView = new HomeSingleView(mContext);
            singleView.setTitle(result,result.getQuestion(),result.getAnswerYdes(),result.getAnswerNdes(),i);
            i++;
            mLinOut.addView(singleView);
            homeSingleViews.add(singleView);
        }
    }

    public String getAnswerJson(String status,
                                String signTime,
                                String homeVisitsType,
                                String visitorContract,
                                String visitTime,
                                String woId,
                                String visitorName ){
        QuestionRequestBean questionRequestBean = new QuestionRequestBean();
        questionRequestBean.setStatus(status);
        questionRequestBean.setSignTime(signTime);
        questionRequestBean.setHomeVisitsType(homeVisitsType);
        questionRequestBean.setVisitorContract(visitorContract);
        questionRequestBean.setVisitTime(visitTime);
        questionRequestBean.setWoId(woId);
        questionRequestBean.setVisitorName(visitorName);
        questionRequestBean.setWoReply(getReply(woId));

        return new Gson().toJson(questionRequestBean);
    }

    private List<QuestionRequestBean.Result> getReply(String wID){
        List<QuestionRequestBean.Result> results = new ArrayList<>();
        for (HomeSingleView homeSingleView :homeSingleViews){
            QuestionRequestBean.Result result = new QuestionRequestBean.Result();
            result.setWoId(wID);
            result.setAnswer(homeSingleView.getAnswer());
            result.setAnswerDes(homeSingleView.getAnswerDes());
            result.setCreateTime("2018-01-01 01:01:01");
            result.setWovisQuestionid(homeSingleView.getPosition()+"");
            results.add(result);
        }
        return results;
    }

    public boolean isAllSel(){
        for (HomeSingleView homeSingleView :homeSingleViews){
            if(!homeSingleView.isSel()){
                return false;
            }
        }
        return true;
    }


}
